package org.lee.mem.app;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Admin
 * 管理员
 *
 * @author dralee
 * @date 2021-11-23 18:14:54
 **/
public class Admin {

    private int index = -1;
    private List<ConfigMemento> mementoList = new ArrayList<>();
    private Map<String, ConfigMemento> mementoMap = new ConcurrentHashMap<>();

    public void append(ConfigMemento memento) {
        mementoList.add(memento);
        mementoMap.put(memento.getConfigFile().getVersionNo(), memento);
        index++;
    }

    public ConfigMemento undo() {
        if (--index <= 0) {
            return mementoList.get(0);
        }
        return mementoList.get(index);
    }

    public ConfigMemento redo() {
        if (++index > mementoList.size()) {
            return mementoList.get(mementoList.size() - 1);
        }
        return mementoList.get(index);
    }

    public ConfigMemento get(String versionNo) {
        return mementoMap.get(versionNo);
    }
}
